'use strict';

module.exports = app => {
  const { STRING, INTEGER, ENUM, BOOLEAN, DATE } = app.Sequelize;

  const User = app.model.define('user', {
    id: { 
      type: INTEGER, 
      primaryKey: true, 
      autoIncrement: true,
      allowNull: false
    },
    username:{
      type: STRING(60),
      allowNull: false
    },
    password:{
      type: STRING,
      allowNull: false
    },
    avatar: {
      type: STRING,
      allowNull: false,
      defaultValue: '/public/img/avatar.jpg'
    },
    background: {
      type: STRING,
      allowNull: false,
      defaultValue: '/public/img/bg1.png'
    },
    type: {
      type: ENUM,
      values: ['admin', 'user'],
      allowNull: false,
      defaultValue: 'user'
    },
    status: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    points: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    days: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    nickname: {
      type: STRING,
      allowNull: true,
    },
    phone: {
      type: STRING,
      allowNull: true,
    },
    description: {
      type: STRING,
      allowNull: true,
      defaultValue: '目前什么也没有~'
    },
    created_at: DATE,
    updated_at: DATE
  });

  User.associate = function () {
    // // 与Info存在一对多关系，所以是hasOne()
    // app.model.Student.hasOne(app.model.Info, {foreignKey: 'studentId'});
    // // 与Classes存在多对一关系，所以使用belongsTo()
    // app.model.Student.belongsTo(app.model.Classes, {foreignKey: 'classId', targetKey: 'id'});
    // // 与Lessison存在多对多关系，使用belongsToMany()
    // app.model.Student.belongsToMany(app.model.Lession, {
    //     through: app.model.LessionStudent,
    //     foreignKey: 'studentId',
    //     otherKey: 'lessionId'
    // });
    app.model.User.hasMany(app.model.Article, {
      foreignKey: {
        name: 'author_id',
        allowNull: true
      }
    })
    app.model.User.hasMany(app.model.Collection, {
      foreignKey: {
        name: 'owner_id',
        allowNull: true
      }
    })
    app.model.User.hasMany(app.model.Task, {
      foreignKey: {
        name: 'sign_id',
        allowNull: true
      }
    })
    app.model.User.belongsToMany(app.model.Test, {
      through: app.model.TestUser,
      foreignKey: 'user_id',
      otherKey: 'test_id'
    })
  }

  return User;
};